package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.common.constant.Constants;
import com.cw.dto.InpatientRegistrationCondition;
import com.cw.dto.InpatientRegistrationDTO;
import com.cw.entity.Bed;
import com.cw.entity.PatientBed;
import com.cw.entity.PatientInfo;
import com.cw.entity.ThisIiPrepayin;
import com.cw.mapper.BedMapper;
import com.cw.mapper.PatientInfoMapper;
import com.cw.mapper2.ThisIiPrepayinMapper;
import com.cw.request.EemptyPatientRequest;
import com.cw.service.IPatientBedService;
import com.cw.service.IPatientInfoService;
import com.cw.util.HttpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 患者信息 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-27
 */
@Service
public class PatientInfoServiceImpl extends ServiceImpl<PatientInfoMapper, PatientInfo> implements IPatientInfoService {

    @Autowired
    private IPatientBedService patientBedService;


    @Autowired
    private BedMapper bedMapper;

    @Resource
    private ThisIiPrepayinMapper thisIiPrepayinMapper;

    /**
     * 根据就诊号和就诊类型获取病人姓名
     *
     * @param visitNo
     * @return
     */
    @Override
    public PatientInfo getPatient(String visitNo, String visitTypeCode) {
        PatientInfo patientInfo = new PatientInfo();
        List<PatientInfo> patients =
                baseMapper.selectList(new LambdaQueryWrapper<>(new PatientInfo()).eq(PatientInfo::getVisitNo,
                visitNo).eq(PatientInfo::getDeleteFlag, "0").eq(PatientInfo::getVisitTypeCode, visitTypeCode));
        if (CollectionUtils.isNotEmpty(patients)) {
            patientInfo = patients.get(0);
        }
        return patientInfo;
    }


    /**
     * 根据病人id获取病人详情
     *
     * @param id
     * @return
     */
    @Override
    public PatientInfo getPatientInfo(String id) {
        PatientInfo patientInfo = baseMapper.selectById(id);
        return patientInfo;
    }

    /**
     * 根据床位id获取病人详情
     *
     * @param bedId
     * @return
     */
    @Override
    public PatientInfo getPatientDetail(String bedId) {
        PatientInfo patientInfo = new PatientInfo();
        PatientBed PatientBed = patientBedService.getVisitNo(bedId);
        if (PatientBed != null) {
            patientInfo = getPatient(PatientBed.getVisitNo(), PatientBed.getVisitTypeCode());
        }
        return patientInfo;
    }


    /**
     * 新增住院登记-手工录入
     * @param patientInfo 新增住院登记
     * @return boolean
     * @author ly
     * @date 2021/4/29 13:52
     */
    @Override
    public boolean insertPatientInfo(PatientInfo patientInfo) {
        String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
        patientInfo.setCreateTime(LocalDateTime.now());
        patientInfo.setCreateBy(userName);

        return save(patientInfo);
    }

    /**
     * 住院登记记录列表
     * @param condition 查询条件
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author ly
     * @date 2021/4/29 14:56
     */
    @Override
    public Map<String, Object> inpatientRegistrationList(InpatientRegistrationCondition condition) {

        //查询急诊
        PageHelper.startPage(condition.getPageNum(), 21);
        condition.setCode("01");
        List<InpatientRegistrationDTO> emergencyCall = baseMapper.inpatientRegistrationDTO(condition);
        PageInfo<InpatientRegistrationDTO> emergencyCallPageInfo = new PageInfo<>(emergencyCall);

        //查询门诊
        PageHelper.startPage(condition.getPageNum(), 21);
        condition.setCode("02");
        List<InpatientRegistrationDTO> outpatient = baseMapper.inpatientRegistrationDTO(condition);
        PageInfo<InpatientRegistrationDTO> outpatientPageInfo = new PageInfo<>(outpatient);


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("emergencyCall", emergencyCallPageInfo);
        resultMap.put("outpatient", outpatientPageInfo);
        return resultMap;
    }

    /**
     * 编辑住院登记
     * @param patientInfo 编辑对象
     * @return boolean
     * @author ly
     * @date 2021/4/30 14:12patientInfoController
     */
    @Override
    public boolean updatePatientInfo(PatientInfo patientInfo) {
        return updateById(patientInfo);
    }

    /**
     * 逻辑删除
     * @param id 主键
     * @author ly
     * @date 2021/4/30 14:22
     * @return boolean
     */
    @Override
    public boolean updateDeleteFlagToZero(String id) {
        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setId(id);
        patientInfo.setDeleteFlag("1");
        return updateById(patientInfo);
    }

    @Override
    public ThisIiPrepayin selectPatientInfoByIdenno(String idenno) {
        ThisIiPrepayin thisIiPrepayin = null;
        //如果它不以MZ开头，并且长度为18位（身份证长度）
        if (!idenno.contains("MZ")&&idenno.length()==18) {
            thisIiPrepayin = thisIiPrepayinMapper.selectPatientInfoByIdenno(idenno);
        } else if (idenno.contains("MZ")){
            thisIiPrepayin = thisIiPrepayinMapper.queryPersonalinformation(idenno);
        }else {
            String x = "MZ0000";
            String ghd = x +idenno;
            thisIiPrepayin = thisIiPrepayinMapper.queryPersonalinformation(ghd);
        }
        return thisIiPrepayin;
    }


    /**
     * 获取没有床位的患者信息
     *要改
     * @param eemptyPatientRequest
     * @return
     */
    @Override
    public List<PatientInfo> getEemptyPatientList(EemptyPatientRequest eemptyPatientRequest) {
        List<PatientInfo> patientInfoList = new ArrayList<>();
        return patientInfoList;
    }

}
